import 'package:flutter/material.dart';
import 'package:flutter_widget/custom/custom_appbar.dart';
import 'package:flutter_widget/model/expansion.dart';

// ignore: must_be_immutable
class ExpansionPanelPage extends StatefulWidget {
  String name;
  ExpansionPanelPage({Key? key, required this.name}) : super(key: key);
  @override
  _ExpansionPanelPageState createState() => _ExpansionPanelPageState();
}

class _ExpansionPanelPageState extends State<ExpansionPanelPage> {
  final List<Expansion> _expansions = Expansions;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: CustomAppbar(widget.name, Theme.of(context).primaryColor),
      body: Container(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            buildExpansionPanelList(),
          ],
        ),
      ),
    );
  }

  Widget buildExpansionPanelList() {
    return ExpansionPanelList(
      expansionCallback: (int panelIndex, bool isExpand) {
        setState(() {
          _expansions[panelIndex].isExpand = !isExpand;
        });
      },
      children: _expansions
          .map(
            (Expansion e) => ExpansionPanel(
              headerBuilder: (BuildContext context, bool isExpand) {
                return Container(
                  padding: const EdgeInsets.all(16.0),
                  child: Text(
                    e.headerText,
                  ),
                );
              },
              body: e.body,
              isExpanded: e.isExpand,
            ),
          )
          .toList(),
    );
  }
}
